//
//  GTRecommendViewController.m
//  news
//
//  Copyright © 2022 sinosun. All rights reserved.
//

#import "GTRecommendViewController.h"

@interface GTRecommendViewController () <UIScrollViewDelegate, UIGestureRecognizerDelegate>

@end

@implementation GTRecommendViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tabBarItem.title = @"推荐";
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollview.backgroundColor = [UIColor lightGrayColor];
    scrollview.contentSize = CGSizeMake(self.view.frame.size.width * 5, self.view.frame.size.height);
    scrollview.showsHorizontalScrollIndicator = NO;
    
    scrollview.delegate = self;
    
    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor blueColor], nil];
    for (int i = 0; i < 5; ++i) {
        [scrollview addSubview:({
            UIView *viwe = [[UIView alloc] initWithFrame:CGRectMake(scrollview.bounds.size.width * i, 0, scrollview.bounds.size.width, scrollview.bounds.size.height)];
            viwe.backgroundColor = colors[i];
            
            [viwe addSubview:({
                UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
                smallView.backgroundColor = [UIColor grayColor];
                // 定义一个手势
                UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smllImageTaped)];
                gestureRecognizer.delegate = self;
                // 为自定义控件添加这个手势
                [smallView addGestureRecognizer:gestureRecognizer];
                smallView;
            })];
            viwe;
        })];
    }
    // 启用翻页
    scrollview.pagingEnabled = YES;

    [self.view addSubview:scrollview];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"UIScrollView did scroll %f", scrollView.contentOffset.x);
}

#pragma mark - UITapGestureRecognizer UIGestureRecognizerDelegate
- (void)smllImageTaped
{
    NSLog(@"smllImageTaped");
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}
@end
